<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>

          <a-col :span="24">
            <a-form-model-item label="字段编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="logicCode">
              <a-input v-model="model.logicCode" placeholder="请输入字段编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="字段名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actCode">
              <a-input v-model="model.logicName" placeholder="请输入字段名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否是KEY" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isKey">
                <a-radio-group  v-model="model.isKey" button-style="solid">
                  <a-radio-button :value="0">
                    否
                  </a-radio-button>
                  <a-radio-button :value="1">
                    是
                  </a-radio-button>
                </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="logicDataType">
              <a-select v-model="model.logicDataType" placeholder="请选择字段类型">
                <!-- ClickHouse字段类型选项 -->
                <<a-select-option v-for="type in fieldTypeOptions" :key="type.value" :value="type.value">{{ type.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数据长度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="logicDataLength">
              <a-input-number v-model="model.logicDataLength" placeholder="请输入数据长度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item  v-if="shouldShowFormItem" label="小数位长度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="logicDataDecimal">
              <a-input-number v-model="model.logicDataDecimal" placeholder="请输入小数位长度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'DatalibLogicActForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      tableInfo: {
        type: Object, // 根据实际数据类型定义
        required: true, // 如果这个prop是必需的，可以设置为true
      },
    },
    data () {
      return {
        fieldTypeOptions:[
          {
            "value": "TINYINT",
            "label": "有符号的8位整数，范围 [-128, 127]"
          },
          {
            "value": "SMALLINT",
            "label": "有符号的16位整数，范围 [-32768, 32767]"
          },
          {
            "value": "INT",
            "label": "有符号的32位整数，范围 [-2^31, 2^31 - 1]"
          },
          {
            "value": "BIGINT",
            "label": "有符号的64位整数，范围 [-2^63, 2^63 - 1]"
          },
          {
            "value": "FLOAT",
            "label": "单精度浮点数"
          },
          {
            "value": "DOUBLE",
            "label": "双精度浮点数"
          },
          {
            "value": "DECIMAL",
            "label": "高精度十进制数，可指定精度和小数位数"
          },
          {
            "value": "BOOLEAN",
            "label": "布尔值，TRUE 或 FALSE"
          },
          {
            "value": "DATE",
            "label": "日期类型，如 '2022-03-26'"
          },
          {
            "value": "DATETIME",
            "label": "日期时间类型，如 '2022-03-26 15:23:45'"
          },
          {
            "value": "VARCHAR",
            "label": "变长字符串类型，需要指定最大长度"
          },
          {
            "value": "STRING",
            "label": "字符串类型，用于存储大量文本数据"
          }
        ],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          logicCode: [{ required: true, message: '请输入字段编码'}],
          logicDataType: [{ required: true, message: '请输入数据类型' }],
        },
        url: {
          add: "/dcloud-platform/api/datalibLogicAct/v1/add",
          edit: "/dcloud-platform/api/datalibLogicAct/v1/edit",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      shouldShowFormItem(){
        return this.model.logicDataType === 'DECIMAL' ||
          this.model.logicDataType === 'FLOAT' ||
          this.model.logicDataType === 'DOUBLE'
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.model.datalibTableId = this.tableInfo.id; //将表的id作为字段表的外键
        console.log(this.model)
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>